﻿Imports System
Imports System.Globalization
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities.Rules
    Friend NotInheritable Class RuleSetReferenceValidator
        Inherits Validator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim reference As RuleSetReference = [TryCast](obj,RuleSetReference)
            If (reference Is Nothing) Then
                Throw New ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.UnexpectedArgumentType, New Object() { GetType(RuleSetReference).FullName, "obj" }), "obj")
            End If
            Dim activity As Activity = [TryCast](manager.Context.Item(GetType(Activity)),Activity)
            If (activity Is Nothing) Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, New Object() { GetType(Activity).Name }))
            End If
            If Not TypeOf manager.Context.Item(GetType(PropertyValidationContext)) Is PropertyValidationContext Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, New Object() { GetType(PropertyValidationContext).Name }))
            End If
            If Not String.IsNullOrEmpty(reference.RuleSetName) Then
                Dim definitions As RuleDefinitions = Nothing
                Dim ruleSets As RuleSetCollection = Nothing
                Dim declaringActivity As CompositeActivity = Helpers.GetDeclaringActivity(activity)
                If (declaringActivity Is Nothing) Then
                    declaringActivity = [TryCast](Helpers.GetRootActivity(activity),CompositeActivity)
                End If
                If (Not activity.Site Is Nothing) Then
                    definitions = ConditionHelper.Load_Rules_DT(activity.Site, declaringActivity)
                Else
                    definitions = ConditionHelper.Load_Rules_RT(declaringActivity)
                End If
                If (Not definitions Is Nothing) Then
                    ruleSets = definitions.RuleSets
                End If
                If ((ruleSets Is Nothing) OrElse Not ruleSets.Contains(reference.RuleSetName)) Then
                    Dim error As New ValidationError(String.Format(CultureInfo.CurrentCulture, Messages.RuleSetNotFound, New Object() { reference.RuleSetName }), &H576) { _
                        .PropertyName = (MyBase.GetFullPropertyName(manager) & ".RuleSetName") _
                    }
                    errors.Add([error])
                    Return errors
                End If
                Dim set As RuleSet = ruleSets.Item(reference.RuleSetName)
                Dim typeProvider As ITypeProvider = DirectCast(manager.GetService(GetType(ITypeProvider)), ITypeProvider)
                Dim checkStaticType As Boolean = False
                Dim service As IWorkflowCompilerOptionsService = [TryCast](manager.GetService(GetType(IWorkflowCompilerOptionsService)),IWorkflowCompilerOptionsService)
                If ((Not service Is Nothing) AndAlso service.CheckTypes) Then
                    checkStaticType = True
                End If
                Dim validation As New RuleValidation(activity, typeProvider, checkStaticType)
                [set].Validate(validation)
                Dim errors2 As ValidationErrorCollection = validation.Errors
                If (errors2.Count > 0) Then
                    Dim fullPropertyName As String = MyBase.GetFullPropertyName(manager)
                    Dim str6 As String = String.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleSetExpression, New Object() { fullPropertyName })
                    Dim errorNumber As Integer = &H577
                    If (Not activity.Site Is Nothing) Then
                        Dim error2 As ValidationError
                        For Each error2 In errors2
                            Dim error3 As New ValidationError(error2.ErrorText, errorNumber) { _
                                .PropertyName = (fullPropertyName & ".RuleSet Definition") _
                            }
                            errors.Add(error3)
                        Next
                        Return errors
                    End If
                    Dim error4 As ValidationError
                    For Each error4 In errors2
                        Dim error5 As New ValidationError((str6 & " " & error4.ErrorText), errorNumber) { _
                            .PropertyName = fullPropertyName _
                        }
                        errors.Add(error5)
                    Next
                End If
                Return errors
            End If
            Dim item As New ValidationError(String.Format(CultureInfo.CurrentCulture, Messages.InvalidRuleSetName, New Object() { "RuleSetReference" }), &H578) { _
                .PropertyName = (MyBase.GetFullPropertyName(manager) & ".RuleSetName") _
            }
            errors.Add(item)
            Return errors
        End Function

    End Class
End Namespace

